package com.novellotus.iov.carvice.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Rich on 2017/8/3.
 * 保存公钥、私钥
 */

public class SharedPreferencesUtils {

    /**
     * 保存登陆时获取的公钥和私钥
     *
     * @param accessKey 公钥
     * @param secretKey 私钥
     */
    public static void saveKey(String accessKey, String secretKey, String uid, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("key", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("accessKey", accessKey);
        editor.putString("secretKey", secretKey);
        editor.putString("uid", uid);
        editor.apply();
    }

    /**
     * 保存用户信息
     */
    public static void saveUser(String id, String mobile, String email, String nickName, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("nickName", nickName);
        editor.putString("mobile", mobile);
        editor.putString("email", email);
        editor.putString("id", id);
        editor.apply();
    }

    /**
     * @param context 获取公钥和私钥
     * @return
     */
    public static String[] getKey(Context context) {
        String[] keys = new String[3];
        SharedPreferences preferences = context.getSharedPreferences("key", Context.MODE_PRIVATE);
        keys[0] = preferences.getString("accessKey", "");
        keys[1] = preferences.getString("secretKey", "");
        keys[2] = preferences.getString("uid", "");
        return keys;
    }

    /**
     * @param mobile 修改手机号码
     */
    public static void changeMobile(String mobile, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("mobile", mobile);
        editor.apply();
    }

    /**
     * @param nickName 修改姓名
     */
    public static void changeUserName(String nickName, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("nickName", nickName);
        editor.apply();
    }

    /**
     * @param context 获取用户信息
     *                0昵称  1电话 2邮箱 3用户id
     * @return
     */
    public static String[] getUser(Context context) {
        String[] keys = new String[4];
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        keys[0] = preferences.getString("nickName", "");
        keys[1] = preferences.getString("mobile", "");
        keys[2] = preferences.getString("email", "");
        keys[3] = preferences.getString("id", "");
        return keys;
    }


    /**
     * 删除缓存的密钥和用户信息
     */
    public static void deleteSharedPreferences(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("key", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.apply();
        SharedPreferences preferencesUser = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        editor = preferencesUser.edit();
        editor.clear();
        editor.apply();
    }

}
